package com.pollux.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * @Author: pollux
 * @CreateTime: 2024-10-30
 * @Description: jwt令牌工具类
 * @Version: 1.0
 */


public class JwtUtil {

    /**
     * @param secretKey 用户密钥
     * @param ttl 令牌过期时间
     * @param claims 令牌携带信息
     * @description: 生成Jwt令牌
     * @return:  jwt令牌
     * @author: pollux
     * @date: 2024/10/30 16:47
     */
    public static String createJwt(String secretKey, Long ttl, Map<String, Object> claims){
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,secretKey)
                .setExpiration(new Date(System.currentTimeMillis() + ttl))
                .setClaims(claims)
                .compact();
    }


    public static Claims parseJwt(String secretKey,String token){
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(token)
                .getBody();
    }


}
